package org.xgluo.nodeutil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author LXG
 * @program: JBOToExcle
 * @name: XNode
 * @description: XMLNode节点类
 * @date 2024年12月02日 10:28
 */
public class XNode implements BaseNode {

    //节点编号
    private String nodeId ;
    //节点内容
    private String nodeTextContent ;
    //节点属性
    private Map<String,String> attrMap ;

    //子节点列表
    private List<XNode> childNodeList;

    //节点级别
    private int nodeLevel ;

    //节点类型
    private short nodeType;

    //是否首节点
    private boolean isFirst;

    //是否末节点
    private boolean isLast;


    XNode(String idNode){
        this.nodeId = idNode;
    }

    /**
     * @description: 是否有子节点
     * @return boolean
     * @date 2024/12/2 10:37
     * @author LXG
     */
    @Override
    public boolean haveChildXNode() {
        return childNodeList != null && childNodeList.isEmpty();
    }


    /**
     * @description: 返回指定层级指定Id的节点（要求不重复），找到一个便停止继续查找并返回
     * @param idNode   判断当前节点及其子节点
     * @param levelNode 节点级别
     * @return XNode
     * @date 2024/12/2 15:34
     * @author LXG
     */
    @Override
    public XNode getXNode(String idNode,int levelNode) {
        if (idNode == null || idNode.isEmpty()){
            return null;
        }
        if(levelNode == nodeLevel || idNode.equals(nodeId)){
            return this;
        }
        if(!(levelNode > nodeLevel)){
            //给定的节点层级不大于当前节点，并且是不是当前节点
            //由于不会出现在子节点中，不需要再遍历子节点查询了，直接返回空
            return null;
        }
        //定义返回的节点对象
        XNode result;
        int childNodeLevel;
        for (XNode childNode : childNodeList) {
            childNodeLevel = childNode.getNodeLevel();
            if(levelNode == childNodeLevel){
                if(idNode.equals(childNode.getNodeId())){
                    return childNode;
                }
            }else if(levelNode > childNodeLevel){  //当前节点层级小于给定的层级，才可以继续查询，否则中断，返回空
                result = getXNode(childNode.getNodeId(),levelNode);
                if(result != null){
                    return result;
                }
            }else {
                return null;
            }
        }
        return null;
    }


    /**
     * @description: 根据层级和节点编号获取子节点信息
     * @param nodeList 节点列表
     * @param idNode 节点编号
     * @param levelNode 节点层级
     * @date 2024/12/2 16:07
     * @author LXG
     */
    @Override
    public void getXNodeList(List<XNode> nodeList, String idNode, int levelNode){
        if (idNode == null || idNode.isEmpty()){
            return;
        }
        if(levelNode == nodeLevel || idNode.equals(nodeId)){
            nodeList.add(this);
        }
        if(!(levelNode > nodeLevel)){
            //给定的节点层级不大于当前节点，并且是不是当前节点
            //由于不会出现在子节点中，不需要再遍历子节点查询了，直接返回空
            return;
        }
        for (XNode childNode : childNodeList) {
            if(levelNode == childNode.nodeLevel){
                if(idNode.equals(childNode.getNodeId())){
                    nodeList.add(childNode);
                }
            } else if (levelNode > childNode.getNodeLevel()) {
                getXNodeList(nodeList, idNode,levelNode);
            }
        }
    }

    /**
     * @description: 根据节点编号获取节点当前节点下子节点集合
     * @param nodeList 需要处理的子节点列表
     * @param idNode 节点编号
     * @date 2024/12/2 16:49
     * @author LXG
     */
    public void getXNodeList(List<XNode> nodeList, String idNode){
        if (idNode == null || idNode.isEmpty()){
            return;
        }
        for (XNode childNode : childNodeList) {
            if(idNode.equals(childNode.getNodeId())){
                nodeList.add(childNode);
            } else if (childNode.haveChildXNode()) {
                getXNodeList(nodeList, idNode);
            }
        }
    }


    /**
     * @description: 获取节点指定的属性
     * @param attributeName 属性名称
     * @return String
     * @date 2024/12/2 16:16
     * @author LXG
     */
    @Override
    public String getAttribute(String attributeName) {
        if(attrMap == null || attrMap.isEmpty()){
            return null;
        }
        return attrMap.get(attributeName);
    }

    /**
     * @description: 根据节点类型获取直系子节点
     * @param typeNode 子节点类型
     * @return List<XNode>
     * @date 2024/12/2 16:37
     * @author LXG
     */
    public List<XNode> getChildByType(short typeNode){
        if(childNodeList == null || childNodeList.isEmpty()){
            return null;
        }
        ArrayList<XNode> childNodeList = new ArrayList<>();
        for (XNode childNode : this.childNodeList) {
            if(typeNode == childNode.getNodeType()){
                childNodeList.add(childNode);
            }
        }
        return childNodeList;
    }

    public String getNodeId() {
        return nodeId;
    }

    public void setNodeId(String nodeId) {
        this.nodeId = nodeId;
    }

    public String getNodeTextContent() {
        return nodeTextContent;
    }

    public void setNodeTextContent(String nodeTextContent) {
        this.nodeTextContent = nodeTextContent;
    }

    public Map<String, String> getAttrMap() {
        return attrMap;
    }

    public void setAttrMap(Map<String, String> attrMap) {
        this.attrMap = attrMap;
    }

    public List<XNode> getChildNodeList() {
        return childNodeList;
    }

    public void setChildNodeList(List<XNode> childNodeList) {
        this.childNodeList = childNodeList;
    }

    public int getNodeLevel() {
        return nodeLevel;
    }

    public void setNodeLevel(int nodeLevel) {
        this.nodeLevel = nodeLevel;
    }

    public short getNodeType() {
        return nodeType;
    }

    public void setNodeType(short nodeType) {
        this.nodeType = nodeType;
    }

    public boolean isFirst() {
        return isFirst;
    }

    public void setFirst(boolean first) {
        isFirst = first;
    }

    public boolean isLast() {
        return isLast;
    }

    public void setLast(boolean last) {
        isLast = last;
    }
}
